
import common.RoleSelector;
import common.SettingPanel;
import common.commonFunction;

import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.managers.PopUpManager;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.validators.Validator;


[Embed(source="images/error.png")]private var errorIcon:Class;
[Embed(source="images/success.png")]private var successIcon:Class;
[Embed(source="images/reset.png")]private var resetIcon:Class;
[Embed(source="images/question.png")]private var questionIcon:Class;

public var params:Object={};
[Bindable] public var universityId:String;
[Bindable] public var userId:String;
[Bindable] public var userGroupId:String;
[Bindable] public var userGroupName:String;
[Bindable]public var urlPrefix:String;
[Bindable] public var userName:String;
[Bindable] public var universityName:String;

var userInfoXml:XML;

//Initilize Login Form
public function initForm():void
{
	urlPrefix=commonFunction.getConstants('url')+"/login/";
	userNameText.setFocus();

}

public function Login():void
{
	if(Validator.validateAll([userNameValidator,passwordValidator]).length==0)
	{
		params["userName"]=userNameText.text;
		params["password"]=passwordText.text;
		//Alert.show(getLoginInfoService.url);
		getLoginInfoService.send(params);
	}
}

public function loginInfoResultHandler(event:ResultEvent):void
{
	userInfoXml=event.result as XML;
	
	var values:ArrayCollection=new ArrayCollection();
	for each(var obj:Object in userInfoXml.loginInfo)
	{
		values.addItem({userId:obj.userId,universityId:obj.universityId,userGroupId:obj.userGroupId,
		userGroupName:obj.userGroupName,userName:obj.userName, universityName:obj.universityName});
	}
	
	if(values.length>0)
	{
		userId=values[0].userId;
		userName=values[0].userName;
		universityId=values[0].universityId;
		universityName=values[0].universityName;
		userNameText.text="";
		passwordText.text="";
		userNameLabel.visible=true;
		
		if(values.length==1)
		{
			userGroupId=values[0].userGroupId;
			userGroupName=values[0].userGroupName;
			getMenues(userGroupId,userGroupName);
		}
		else
		{
			var selectRolectPopup:RoleSelector=RoleSelector(PopUpManager.createPopUp(this,RoleSelector,true));
			selectRolectPopup.roleCombo.dataProvider=userInfoXml.loginInfo.userGroupName;
			selectRolectPopup.roleCombo.selectedIndex=-1;
			selectRolectPopup.dataXml=userInfoXml;
			selectRolectPopup.refFunction=getMenues;
			PopUpManager.centerPopUp(selectRolectPopup);
		}
	}
	else
	{
		Alert.show("Invalid User Name and Password");
	}
}

public function getMenues(roleId:String,roleName:String):void
{
	var param:Object=new Object();
	param["userGroupId"]=roleId;
	param["userGroupName"]=roleName;
	menuHttpService.send(param);
}

public function loginInfoFaultHandler(event:FaultEvent):void
{
	Alert.show(event.message+"");
}

public function logoutResultHandler(event:ResultEvent):void
{
	logout();
}

public function logoutFaultHandler(event:FaultEvent):void
{
	logout();
}

public function logout():void
{
	Alert.show(commonFunction.getMessages('successOnLogout'),commonFunction.getMessages('success'),0,null,null,successIcon);
	userInfoXml=new XML;
	vStack.selectedIndex=0;
	userNameLabel.text="";
	userNameLabel.visible=false;
	loaderCanvas.removeAllChildren();
	userNameText.setFocus();
}

public function goToSettings():void
{
	loaderCanvas.removeAllChildren();
	loaderCanvas.addChild(new SettingPanel);
}